package cn.wdm.system.controller;

import cn.wdm.basic.util.PageList;
import cn.wdm.basic.util.AjaxResult;
import cn.wdm.system.domain.Dictionary;
import cn.wdm.system.domain.DictionaryDetail;
import cn.wdm.system.query.DictionaryQuery;
import cn.wdm.system.service.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/dict")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;
    /**
     * 1.高级查询和分页查询
     * @return
     * dictionaryQuery 高级查询和分页查询的条件
     * @RequestBody 表示在请求题里面去拿数据，将数据封装到DictionaryQuery对象中
     */
    @PostMapping("/queryPage")
    public PageList<Dictionary> queryPage(@RequestBody DictionaryQuery dictionaryQuery){
        return dictionaryService.queryPage(dictionaryQuery);
    }

    /**
     * 2.查询全部
     * @return 返回dictionary集合
     */
    @GetMapping
    public List<Dictionary> queryAll(){
        return dictionaryService.queryAll();
    }
    /**
     * 3.查询一条数据
     * @param id
     * @return 返回dictionary对象
     */
    @GetMapping("/{id}")
    public Dictionary queryById(@PathVariable("id") Long id){
        return dictionaryService.queryById(id);
    }
    @PostMapping("/{id}")
    public List<DictionaryDetail> get(@PathVariable("id") Long id){
        return dictionaryService.loadByDictionaryId(id);
    }
    /**
     * 新增和修改
     * @param dictionary
     * @return
     */
    @PutMapping("/addOrupd")
    public AjaxResult addOrupd(@RequestBody Dictionary dictionary){
        try {
            if (dictionary.getId() == null) {
                dictionaryService.save(dictionary);
            }else{
                dictionaryService.update(dictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    @PatchMapping
    public AjaxResult deleteBatch(@RequestBody List<Long> ids){
        try {
            dictionaryService.removeBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
